package hsky.mk.Interceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class BaseInterceptor extends HandlerInterceptorAdapter {
    public BaseInterceptor(){
        System.out.println("拦截器构造器执行");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Map <String, String[]>  properties = request.getParameterMap();
        Iterator<Entry<String, String[]>> iter = properties.entrySet().iterator();
        String name = "";
        String value = "";
        HashMap paraMap = new HashMap();
        while (iter.hasNext()) {
            Entry<String, String[]> entry = iter.next();
            name = entry.getKey();
            Object valueObj = entry.getValue();

            if (null == valueObj) {
                value = "";

            }else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }

            }else {
                value = valueObj.toString(); //用于请求参数中请求参数名唯一

            }
            paraMap.put(name,value);

        }
        System.out.println(paraMap);
        return  false;

    }
}
